package com.hcs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcs.comm.R;
import com.hcs.entity.Category;
import com.hcs.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hcs
 * @since 2025-01-09 04:10
 */
@RestController
@RequestMapping("/category")
@Tag(name = "CategoryController", description = "分类管理")
@CrossOrigin
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    //展示数据
    @GetMapping("/getList")
    @Operation(summary = "展示数据")
    public ResponseEntity<R<List<Category>>> list() {
        List<Category> list = categoryService.list();
        return ResponseEntity.ok(R.success(list));
    }

    //根据名称查询
    @GetMapping("/list")
    @Operation(summary = "根据名称查询")
    public ResponseEntity<R<List<Category>>> list(String name) {
        List<Category> list = categoryService.list(name);
        return ResponseEntity.ok(R.success(list));
    }

    //添加分类
    @PostMapping("/save")
    @Operation(summary = "添加分类")
    public ResponseEntity<R<String>> save(@RequestBody Category category) {
        categoryService.saveWithDish(category);
        return ResponseEntity.ok(R.success("添加成功"));
    }

    //修改分类
    @PutMapping("/update")
    @Operation(summary = "修改分类")
    public ResponseEntity<R<String>> update(@RequestBody Category category) {
        categoryService.updateWithDish(category);
        return ResponseEntity.ok(R.success("修改成功"));
    }

    //删除分类
    @DeleteMapping("/remove/{id}")
    @Operation(summary = "删除分类")
    public ResponseEntity<R<String>> remove(@PathVariable Long id) {
        categoryService.removeWithDish(id);
        return ResponseEntity.ok(R.success("删除成功"));
    }

    //点击详情
    @GetMapping("/{id}")
    @Operation(summary = "点击详情")
    public ResponseEntity<R<Category>> get(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return ResponseEntity.ok(R.success(category));
    }

    //分页
    @GetMapping("/page")
    @Operation(summary = "分页")
    public ResponseEntity<R<Page<Category>>> page(Integer page, Integer pageSize, String name) {
        R<Page<Category>> re = categoryService.page(page, pageSize,name);
        return ResponseEntity.ok(re);
    }
}
